package com.zhiming.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhiming.common.exception.pojo.ExceptionEnum;
import com.zhiming.common.exception.pojo.ZmException;
import com.zhiming.item.mapper.MenuMapper;
import com.zhiming.item.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 导航分类 Service
 */

@Service
public class MenuService {
    @Autowired(required = false)
    private MenuMapper menuMapper;

    /**
     * 获取导航条菜单
     */
    public List<Menu> findMenuList() {
        //1.封装条件
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        //通过排序码排序
        queryWrapper.orderByAsc("sort_code");

        //2.执行查询，获取结果
        List<Menu> menuList = menuMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(menuList)){
            throw new ZmException(ExceptionEnum.MENU_DETAIL_NOT_FOUND);
        }

        //3.返回结果
        return menuList;
    }

    public Menu findMenuById(Integer id) {
        Menu menu = menuMapper.selectById(id);
        if(menu == null){
            throw new ZmException(ExceptionEnum.MENU_DETAIL_NOT_FOUND);
        }
        return menu;

    }
}
